/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIESREGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability 
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2015, 2016 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : r_cg_icu_user.c
* Version      : Code Generator for RX23T V1.00.04.02 [29 Nov 2016]
* Device(s)    : R5F523T5AxFM
* Tool-Chain   : CCRX
* Description  : This file implements device driver for ICU module.
* Creation Date: 2017/8/2
***********************************************************************************************************************/

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_icu.h"
/* Start user code for include. Do not edit comment generated here */
#include "all.h"

/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
Msg_SmpToCtrl_t* Ptemp=(Msg_SmpToCtrl_t*)IORxBuf;
uint8_t cnt=0;
uint8_t IO_DATA_OK=0;//采集板正确帧标志位，表示数组中这一帧完全正确，可以直接使用

/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
* Function Name: r_icu_irq2_interrupt
* Description  : None
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
#if FAST_INTERRUPT_VECTOR == VECT_ICU_IRQ2
#pragma interrupt r_icu_irq2_interrupt(vect=VECT(ICU,IRQ2),fint)
#else
#pragma interrupt r_icu_irq2_interrupt(vect=VECT(ICU,IRQ2))
#endif
static void r_icu_irq2_interrupt(void)
{
    /* Start user code. Do not edit comment generated here */

		P24_OUT0;//确保在接收没完成的时候发送方不会再发送新的数据
		IORxBuf[cnt] = PORT4.PIDR.BYTE;
		cnt++;
		if (cnt==sizeof(Msg_SmpToCtrl))//装满了一帧
		{
			//检查是否符合要求，不符合的话就23，否则跳出
			if((0xAAAA == Ptemp->Pkg.St) && (0x5555 == Ptemp->Pkg.Sp))//符合要求
			{
			cnt=0;
			IO_DATA_OK=1;//标志位置一，在主循环里可以直接使用采集板传上来的数据了，也就是把缓存数组存到结构体里
			return;
			}
			else//纠正错误
			{
				uint8_t i=0;

                for(i=0; i<sizeof(Msg_SmpToCtrl)-1; i++)

				 if((0xAA == Ptemp->Buf[i]) && (0xAA == Ptemp->Buf[i+1]))
				{

					 cnt=sizeof(Msg_SmpToCtrl)-i;
					 for(i=0; i<sizeof(Msg_SmpToCtrl)-1; i++)
						 Ptemp->Buf[i]=0;
					break;
				}
                 else if((0x55 == Ptemp->Buf[i]) && (0x55 == Ptemp->Buf[i+1]))
                 {
                	 cnt=sizeof(Msg_SmpToCtrl)-i-2;
					 for(i=0; i<sizeof(Msg_SmpToCtrl)-1; i++)
						 Ptemp->Buf[i]=0;
                     break;
                 }
			}

		}
		P24_OUT1;//接受已完成，发送方可以发送新的数据
    /* End user code. Do not edit comment generated here */
}

/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
